package projeto.view;

import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import projeto.controller.ControleDeUsuarios;
import projeto.model.*;

/**
 * Painel principal do projeto
 * @author Javatar
 */
@SuppressWarnings("serial")
public class FramePrincipal extends javax.swing.JFrame {
    private static JPanel painelArquivo,painelPerfis,painelCadastro,painelRecomendacoes,painelEstabelecimentos;
    protected static ControleDeUsuarios userControl;
    protected static List<Estabelecimento> estabelecimentos;
    protected static boolean isOnline;
    private JLabel back;
    /** Creates new form FramePrincipal */
    public FramePrincipal() {
        painelArquivo = new PainelArquivo();
        back = new JLabel();
        this.add(back);
        isOnline = false;
        initComponents();
      this.setResizable(false);
        
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("Arquivo");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Importar");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem7.setText("Salvar Operações");
        jMenu1.add(jMenuItem7);

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem8.setText("Sair");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem8);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ações");

        jMenu4.setText("Ações de Estabelecimento");

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem5.setText("Exibir Estabelecimentos");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem5);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem6.setText("Ranking Estabelecimentos");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem6);

        jMenu2.add(jMenu4);

        jMenu3.setText("Ações de Usuario");
        jMenu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu3ActionPerformed(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText("Exibir Perfis");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem3.setText("Gerar Recomendações");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem4.setText("Cadastrar Usuário");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenu2.add(jMenu3);

        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem9.setText("Comparar Algoritmos");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem9);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        adicionaPainelJTabbedPane1("Importar Arquivo",painelArquivo);
        
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu3ActionPerformed
           
    }//GEN-LAST:event_jMenu3ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        if(userControl == null){
            JOptionPane.showMessageDialog(null,"Voc� deve selecionar os arquivos primeiro!","Erro", JOptionPane.ERROR_MESSAGE);
        }else{
            if(!PainelPerfis.aberto){
                painelPerfis = new PainelPerfis(LeituraDeDados.geraMatrizDados(userControl.getListaDeUsuarios()));
                
                adicionaPainelJTabbedPane1("Perfis de Usuario", painelPerfis);
                PainelPerfis.aberto = true;
            }
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed

       if(userControl == null){
            JOptionPane.showMessageDialog(null,"Voc� deve selecionar os arquivos primeiro!","Erro", JOptionPane.ERROR_MESSAGE);
        }
        else{
           if(!PainelControleRecomendacoes.aberto){
                painelRecomendacoes = new PainelControleRecomendacoes();
                adicionaPainelJTabbedPane1( "Recomenda��es", painelRecomendacoes);
                PainelControleRecomendacoes.aberto = true;
           }
        }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
       if(userControl == null){
            JOptionPane.showMessageDialog(null,"Voc� deve selecionar os arquivos primeiro!","Erro", JOptionPane.ERROR_MESSAGE);
        }else{
           if(!PainelCadastro.aberto){
                painelCadastro = new PainelCadastro();
                adicionaPainelJTabbedPane1("Cadastrar Usu�rio", painelCadastro);
                PainelCadastro.aberto = true;
           }
       }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        
        System.exit(0);
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
         if(userControl == null){
            JOptionPane.showMessageDialog(null,"Voc� deve selecionar os arquivos primeiro!","Erro", JOptionPane.ERROR_MESSAGE);
        }else{
           if(!PainelEstabelecimentos.aberto){
               painelEstabelecimentos = new PainelEstabelecimentos(LeituraDeDados.geraMatrizDadosEsts(estabelecimentos));
               adicionaPainelJTabbedPane1("Estabelecimentos", painelEstabelecimentos);
               PainelEstabelecimentos.aberto = true;
           }
        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        if(userControl == null){
            JOptionPane.showMessageDialog(null,"Voc� deve selecionar os arquivos primeiro!","Erro", JOptionPane.ERROR_MESSAGE);
        }else{
                if(!PainelRankingEstabelecimentos.aberto){
                	Recomenda Popularidade = RecomendaPopularidade.getInstance();
                	List<Estabelecimento> estabelecimentosPop = Popularidade.getRecomendacao(FramePrincipal.estabelecimentos, FramePrincipal.userControl.getListaDeUsuarios(),null,39);
                	FramePrincipal.adicionaPainelJTabbedPane1("Recomenda��es", new PainelRankingEstabelecimentos(LeituraDeDados.geraMatrizDadosEstsPopu(estabelecimentosPop)));
                	PainelRankingEstabelecimentos.aberto = true;
            }
        }
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        if(userControl == null){
            JOptionPane.showMessageDialog(null,"Voc� deve selecionar os arquivos primeiro!","Erro", JOptionPane.ERROR_MESSAGE);
        }else{
            if(!PainelComparaAlgoritmos.aberto){
                try{
                    int qntd_rec = Integer.parseInt(JOptionPane.showInputDialog("Quantidade de recomendações"));
                    FramePrincipal.adicionaPainelJTabbedPane1("Painel Comparação", new PainelComparaAlgoritmos(qntd_rec));
                    PainelComparaAlgoritmos.executa();
                    
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }//GEN-LAST:event_jMenuItem9ActionPerformed
    
    public static void criaEstabelecimentos(String localArquivo){
        estabelecimentos = LeituraDeDados.lerEstabelecimentos(localArquivo);
        LeituraDeDados.verificaTabelas(estabelecimentos);
    }
    public static void setUserControl(ControleDeUsuarios userControl){
        FramePrincipal.userControl = userControl;
    }
    public static void removePainelJTabbedPane1(){
        
        userControl = null;
        estabelecimentos = null;
        
        
    }
    public static void adicionaPainelJTabbedPane1(String titulo,JPanel painel){
            
            jTabbedPane1.addTab(titulo, null, painel, "");
      
             jTabbedPane1.setSelectedComponent(painel);  
            int i = jTabbedPane1.getSelectedIndex();  
            jTabbedPane1.setTabComponentAt(i, new ButtonTabComponent(jTabbedPane1));
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FramePrincipal().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    protected static javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}
